package Interceptors;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import java.util.Map;

public class LoginStateInterceptor extends AbstractInterceptor {
    private String user_name;
    private String book_id;

    public String getBook_id() {
        return book_id;
    }

    public void setBook_id(String book_id) {
        this.book_id = book_id;
    }

    private boolean isLogin = false;

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Map context = ActionContext.getContext().getSession();
        Object loginState = context.get("isLogin");
        this.setLogin(loginState == null ? false : (boolean) loginState);
        String result;
        if (isLogin)
            result = actionInvocation.invoke();
        else
            result = "needLogin";
        return result;
    }
}
